package cn.qiao.mdc.util;

import cn.qiao.mdc.consts.Constants;
import org.slf4j.MDC;

public class Util {

    public static boolean isBlank(String str) {
        return str == null || str.isBlank();
    }

    public static <T> T trace(Supplier<T> supplier) throws Throwable {
        String traceId = MDC.get(Constants.TRACE_ID);
        boolean put = false;
        try {
            if (isBlank(traceId)) {
                MDC.put(Constants.TRACE_ID, Constants.getTraceId());
                put = true;
            }
            return supplier.get();
        } finally {
            if (put) {
                MDC.clear();
            }
        }
    }

    public interface Supplier<T> {
        T get() throws Throwable;
    }
}
